package com.wz.project.inStock.stockin.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.wz.project.inStock.stockin.mapper.InStockinFinishMapper;
import com.wz.project.inStock.stockindetail.mapper.InStockinDetailFinishMapper;
import com.wz.project.inStock.stockindetail.mapper.InStockinDetailMapper;
import com.wz.project.sap.DN.service.IZflWmsGetDnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wz.project.inStock.stockin.mapper.InStockinMapper;
import com.wz.project.inStock.stockin.domain.InStockin;
import com.wz.project.inStock.stockin.service.IInStockinService;
import com.wz.common.utils.text.Convert;

/**
 * 入库单据Service业务层处理
 * 
 * @author wz
 * @date 2022-09-07
 */
@Service
public class InStockinServiceImpl implements IInStockinService 
{
    @Autowired
    private InStockinMapper inStockinMapper;

    @Autowired
    private InStockinFinishMapper inStockinFinishMapper;
    @Autowired
    private InStockinDetailMapper inStockinDetailMapper;
 @Autowired
 private InStockinDetailFinishMapper inStockinDetailFinishMapper;
    @Autowired
    private IZflWmsGetDnService zflWmsGetDnService;

    /**
     * 查询入库单据
     * 
     * @param stockinGuid 入库单据ID
     * @return 入库单据
     */
    @Override
    public InStockin selectInStockinById(String stockinGuid)
    {
        return inStockinMapper.selectInStockinById(stockinGuid);
    }

    /**
     * 查询入库单据列表
     * 
     * @param inStockin 入库单据
     * @return 入库单据
     */
    @Override
    public List<InStockin> selectInStockinList(InStockin inStockin)
    {
        if(Integer.valueOf(inStockin.getStatus())<5){
            return inStockinMapper.selectInStockinList(inStockin);

        }else{
            return inStockinFinishMapper.selectInStockinFinishList(inStockin);
        }
    }

    /**
     * 新增入库单据
     * 
     * @param inStockin 入库单据
     * @return 结果
     */
    @Override
    public int insertInStockin(InStockin inStockin)
    {
        return inStockinMapper.insertInStockin(inStockin);
    }

    /**
     * 修改入库单据
     * 
     * @param inStockin 入库单据
     * @return 结果
     */
    @Override
    public int updateInStockin(InStockin inStockin)
    {
        return inStockinMapper.updateInStockin(inStockin);
    }

    /**
     * 删除入库单据对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteInStockinByIds(String ids)
    {
        return inStockinMapper.deleteInStockinByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除入库单据信息
     * 
     * @param stockinGuid 入库单据ID
     * @return 结果
     */
    @Override
    public int deleteInStockinById(String stockinGuid)
    {
        return inStockinMapper.deleteInStockinById(stockinGuid);
    }

    /**
     * 单据审核
     * @param stockinGuid
     * @return
     */
    @Override
    public int audit(String stockinGuid) {
        InStockin stockinManage = inStockinMapper.selectInStockinById(stockinGuid);
        if ("1".equals(stockinManage.getStatus())){
            stockinManage.setStatus("2");
            inStockinMapper.updateInStockin(stockinManage);
            return 1;
        }else {
            return 0;
        }
    }
    /**
     * 手动单据确认
     * @param stockinGuid
     * @return
     */
    @Override
    public int receiptSure(String stockinGuid) {
        InStockin stockin = inStockinMapper.selectInStockinById(stockinGuid);
        if("5".equals(stockin.getStatus())){
            stockin.setStatus("6");
            //更新单据状态
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date=sdf.format(new Date());
            stockin.setFinishDate(date);
            inStockinMapper.updateInStockin(stockin);
            inStockinFinishMapper.insertInStockinFinish(stockin);
            inStockinDetailFinishMapper.insertInStockinDetailFinishById(stockinGuid);
            inStockinMapper.deleteInStockinById(stockinGuid);
            inStockinDetailMapper.deleteStockinDetailByGuId(stockinGuid);
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * 单据作废
     * @param stockinGuid
     * @return
     */
    @Override
    public int receiptCancle(String stockinGuid) {
        InStockin stockin = inStockinMapper.selectInStockinById(stockinGuid);
        if ("1".equals(stockin.getStatus()) || "2".equals(stockin.getStatus())){
            stockin.setStatus("7");
            //更新单据状态
            inStockinMapper.updateInStockin(stockin);
            inStockinFinishMapper.insertInStockinFinish(stockin);
            inStockinDetailFinishMapper.insertInStockinDetailFinishById(stockinGuid);
            inStockinMapper.deleteInStockinById(stockinGuid);
            inStockinDetailMapper.deleteStockinDetailByGuId(stockinGuid);
            zflWmsGetDnService.deleteZflWmsGetDnById(stockinGuid);
            return 1;
        }else{
            return 0;
        }
    }
}
